#include "NamedDataBase.h"
#include <pathinfo.h>
 
CMyDBClass::~CMyDBClass() {
	iItemsDatabase.Close();
	iFsSession.Close();
}

CMyDBClass* CMyDBClass::NewL() {
	CMyDBClass* self = new (ELeave) CMyDBClass();
	CleanupStack::PushL(self);
	self->ConstructL();
	CleanupStack::Pop();
	return self;
}


void CMyDBClass::ConstructL()
{
	User::LeaveIfError(iFsSession.Connect());
	DBFileName.Copy(PathInfo::PhoneMemoryRootPath());
	User::LeaveIfError(iFsSession.SessionPath(DBFileName));
	User::LeaveIfError(iFsSession.CreatePrivatePath(EDriveC));
	iFsSession.PrivatePath(DBFileName);
	DBFileName.Append(KSettDatabaseName);
	
	
	
	//BaflUtils::DeleteFile(iFsSession, DBFileName);

	if(BaflUtils::FileExists(iFsSession, DBFileName)) {
		
		User::LeaveIfError(iItemsDatabase.Open(iFsSession,DBFileName));
	}
	else {	// no database exists so we make one
	
		User::LeaveIfError(iItemsDatabase.Create(iFsSession,DBFileName));
		CreateTableL();
	}
}
void CMyDBClass::OpenTableLC(const TDesC& aTableName) {
	QueryBuffer.Copy(_L("SELECT * FROM "));
	QueryBuffer.Append(aTableName);
	MyView.Prepare(iItemsDatabase,TDbQuery(QueryBuffer));
	CleanupClosePushL(MyView);
	MyView.EvaluateAll();
	MyView.FirstL();	
}

TBool CMyDBClass::FindInTableL(const TDesC& aName) {
	while(MyView.AtRow()) {	
		MyView.GetL();		
		if(aName.Compare(MyView.ColDes(2)) == 0) {
			return ETrue;
		}
		MyView.NextL();
	} 	
	return EFalse;
}

void CMyDBClass::CreateTableL() 
	{
	// Create Des table definition
	CDbColSet* columns=CDbColSet::NewLC();	
	// Add Columns
	TDbCol id(NSettCol0,EDbColInt32);
	id.iAttributes=id.EAutoIncrement;

	// Create Des Table
	columns->AddL(id);				 	
	columns->AddL(TDbCol(NSettCol1, EDbColText16,200));
	columns->AddL(TDbCol(NSettCol2, EDbColInt64));
	columns->AddL(TDbCol(NSettCol3, EDbColReal32));
	columns->AddL(TDbCol(NSettCol4, EDbColInt32));
	
	User::LeaveIfError(iItemsDatabase.CreateTable(KSettDesTable, *columns));
	CleanupStack::PopAndDestroy(columns);

	

}
 
TBool CMyDBClass::ReadDbItemsL(const TDesC& aName, TInt32 &aValue) {
	/*OpenTableLC(KSettIntTable);	
	if(FindInTableL(aName)) {
		aValue = MyView.ColInt32(3);
		CleanupStack::PopAndDestroy(1); // MyView
		return ETrue;
	}
	else {
		CleanupStack::PopAndDestroy(1); // MyView
		return EFalse;	
	}*/
}

TBool CMyDBClass::ReadDbItemsL(const TDesC& aNote, TInt& aCat,TReal32& aExp,TInt64& aDate) {

	OpenTableLC(KSettDesTable);
	if(FindInTableL(aNote)) {
		
		aCat = MyView.ColInt(5);
		aExp = MyView.ColReal(4);
		aDate = MyView.ColInt64(3);
		CleanupStack::PopAndDestroy(1); // MyView
		return ETrue;
	}
	else {
		CleanupStack::PopAndDestroy(1); // MyView
		return EFalse;	
	}
}
void CMyDBClass::ReadDbItemsL()
{	
	/*OpenTableLC(KSettDesTable); 
	while(MyView.AtRow()) 
	{	
		MyView.GetL();		
        TBuf<100> toPrint;
        toPrint.Zero();
        toPrint.AppendNum(MyView.ColInt(1));
        toPrint.Append(_L(":"));
        toPrint.Append(MyView.ColDes(2));
        toPrint.Append(_L(":"));
        toPrint.Append(MyView.ColDes(3));
        
		MyView.NextL();
	} 
	
	CleanupStack::PopAndDestroy(1); // MyView

	OpenTableLC(KSettIntTable); 
	// Just delete one instance of the message
	while(MyView.AtRow()) 
	{	
		MyView.GetL();		
        TBuf<100> toPrint;
        toPrint.Zero();
        toPrint.AppendNum(MyView.ColInt(1));
        toPrint.Append(_L(":"));
        toPrint.Append(MyView.ColDes(2));
        toPrint.Append(_L(":"));
        toPrint.AppendNum(MyView.ColInt32(3));
        
		MyView.NextL();
	} 
	CleanupStack::PopAndDestroy(1); // MyView*/
}
 
TBool CMyDBClass::UpdateDatabaseL(const TDesC& aNote, TInt& aCat,TReal32& aExp,TInt64& aDate)
{	
	OpenTableLC(KSettDesTable);	
	iItemsDatabase.Begin();
	
	if(FindInTableL(aNote) == EFalse) {
		MyView.InsertL(); 		
		MyView.SetColL(2, aNote);

		MyView.SetColL(5, aCat);
		MyView.SetColL(4, aExp);
		MyView.SetColL(3, aDate);
		MyView.PutL();			
	}
	CleanupStack::PopAndDestroy(1); // MyView
	iItemsDatabase.Commit();	
}


void CMyDBClass::UpdateDatabaseL(const TDesC& aName, const TInt32 aValue) {
	/*OpenTableLC(KSettIntTable);	
	iItemsDatabase.Begin();

	if(FindInTableL(aName)) {
		MyView.UpdateL();
	}
	else {
		MyView.InsertL(); 		
		MyView.SetColL(2, aName);
	}
	MyView.SetColL(3, aValue);	
	MyView.PutL();			
	CleanupStack::PopAndDestroy(1); // MyView
	iItemsDatabase.Commit();*/
}
